//
//  ViewController.m
//  shop
//
//  Created by student on 16/2/28.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import "ShopView.h"
#import "ShopPlist.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *backView;
@property (nonatomic,strong) NSMutableArray *shops;

@end

@implementation ViewController

//懒加载 读取plist文件
- (NSArray *)shops
{
    if(_shops == nil)
    {
        _shops = [[NSMutableArray alloc] init];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"goods" ofType:@"plist"];
        NSArray *arr = [NSArray arrayWithContentsOfFile:path];
        for (NSDictionary *temp in arr)
        {
            ShopPlist *shopPlist = [[ShopPlist alloc] initWithDictionary:temp];
            [_shops addObject:shopPlist];
        }
    }
    return _shops;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    int X=5,Y=5;
    int width = 200,height = 170;
    int distance = self.backView.frame.size.width-2*(width+X);
    
    for (int i=0; i<self.shops.count; i++)
    {
        ShopView *shopView = [[ShopView alloc] init];
        shopView.frame = CGRectMake(X, Y, width, height);
        shopView.backgroundColor = [UIColor whiteColor];
        [shopView setShop:self.shops[i]];
        [self.backView addSubview:shopView];
        X += width+distance;
        if ((i+1)%2 == 0)
        {
            X = 5;
            Y += height+distance;
        }
    }
}

@end
